package org.budo.beetl.resource.classpath;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.beetl.core.Resource;
import org.beetl.core.ResourceLoader;
import org.budo.support.spring.io.util.ResourceUtil;
import org.springframework.core.io.ClassPathResource;

import lombok.Getter;
import lombok.Setter;

/**
 * @author lmw
 */
@Getter
@Setter
public class BudoClassPathBeetlResource extends Resource {
    private String root;

    public BudoClassPathBeetlResource(String id, ResourceLoader loader) {
        super(id, loader);
    }

    public BudoClassPathBeetlResource(String id, ResourceLoader loader, String root) {
        this(id, loader);
        this.setRoot(root);
    }

    @Override
    public boolean isModified() {
        return true;
    }

    @Override
    public Reader openReader() {
        ClassPathResource resource = this.toSpringResource();
        InputStream inputStream = ResourceUtil.getInputStream(resource);
        return new InputStreamReader(inputStream);
    }

    public ClassPathResource toSpringResource() {
        return new ClassPathResource(this.getRoot() + this.getId());
    }

    @Override
    public String toString() {
        return super.toString() + ", root=" + this.getRoot() + ", id=" + this.getId() + ", resourceLoader" + this.getResourceLoader();
    }
}
